/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić              *
 *   ivasic@gmail.com                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "log.h"
#include "globals.h"
#include "trackerdb.h"
#include "trackerserver.h"

#include "exceptions.h"

#include "../gui/settingswidget.h"

#include <QtCore>
#include <QtGui>
#include <QApplication>
#include <QMessageBox>

Globals* Globals::m_instance = 0l;

Globals::Globals()
		: m_server(0l), m_db(0l), m_log(0l), m_settings(0l)
{
	//brisi objekat kad izlazim iz aplikacije
	connect(qApp, SIGNAL(destroyed()), this, SLOT(kill()));

	m_settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "Ivan", "QTracker", this);
	m_log = new Log(this);

	try
	{
		m_db = new TrackerDB(
				   (TrackerDB::DBMS)m_settings->value("dbms", TrackerDB::DB_MYSQL).toInt(),
				   /* m_settings->value("db_name", "allfr_1030968_qtracker").toString(),
				   m_settings->value("db_host", "sql2.allfreehosting4you.info").toString(),
				   m_settings->value("db_port", 3306).toInt(),
				   m_settings->value("db_user", "allfr_1030968").toString(),
				   m_settings->value("db_password", "testpass").toString(),*/
				   m_settings->value("db_name", "qtracker").toString(),
				   m_settings->value("db_host", "localhost").toString(),
				   m_settings->value("db_port", 3306).toInt(),
				   m_settings->value("db_user", "root").toString(),
				   m_settings->value("db_password", "").toString(),
				   m_settings->fileName(),
				   this);

	}
	catch (DBException& ex)
	{
		throw ex;
		return;
	}


	m_server = new TrackerServer(this);


	///@TODO Update interval 1sec?
	m_timer.setInterval(1000);

	connect(&m_timer, SIGNAL(timeout()), m_db, SLOT(onTimeout()));

	m_timer.start();
}


Globals::~Globals()
{
	qDebug() << "~Globals()";

	//prvo diskonektuj
	disconnect(&m_timer, SIGNAL(timeout()), m_db, SLOT(onTimeout()));

	//onda izgasi tajmer da be dodje do havarije...
	m_timer.stop();

	if (m_server)
		delete m_server;

	if (m_db)
		delete m_db;

	//Vrati log handler QT-u pre nego ubijem Log objekat!
	qInstallMsgHandler(0);

	if (m_log)
		delete m_log;

	if (m_settings)
		delete m_settings;
}

Globals & Globals::instance()
{
	if (!m_instance)
	{
		//@PATTERN: Double-Check Locking pattern
		QMutex m;
		QMutexLocker lock (&m);

		if (!m_instance)
			m_instance = new Globals();
	}

	return *m_instance;
}

void Globals::Destroy()
{
	if (m_instance)
	{
		//@PATTERN: Double-Check Locking pattern
		QMutex m;
		QMutexLocker lock (&m);

		if (m_instance)
			delete m_instance;

		m_instance = 0l;
	}
}

void Globals::kill()
{
	Destroy();
}
